package com.imy.luascript.app.ui.main;

import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import androidx.viewpager.widget.ViewPager;

import com.imy.common.base.adapter.ViewPagerAdapter;
import com.imy.common.util.SPUtil;
import com.imy.luascript.R;
import com.imy.luascript.app.ui.main.fragment.index.IndexFragment;
import com.imy.luascript.app.ui.main.fragment.setting.SettingFragment;
import com.imy.luascript.base.activity.BaseViewActivity;
import com.imy.luascript.constant.SPConstant;
import com.imy.luascript.utils.ToolBarBuilder;
import com.imy.luascript.view.navigation.NavigationItem;
import com.imy.luascript.view.navigation.NavigationView;
import com.imy.luascript.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseViewActivity {
    @BindView(R.id.navigation_view)
    NavigationView navigationView;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private PowerManager.WakeLock wakeLock;
    private static final String WAKE_LOCK_TAG = "LuaScript:ScreenKeeper";
    private static final long MAX_TEST_DURATION = 24 * 60 * 60 * 1000;
    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected ToolBarBuilder getToolBarBuilder() {
        ToolBarBuilder.Builder builder = new ToolBarBuilder.Builder();
        builder.setHasToolbar(false);
        return builder.build();
    }
    private List<Fragment> getFragment() {
        List<Fragment> list=new ArrayList<>();
        list.add(new IndexFragment());
        list.add(new SettingFragment());
        return list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initWakeLock();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        initNavigation();
        initTabFragment();

    }

    private void initWakeLock() {
        boolean aBoolean = SPUtil.getBoolean(SPConstant.WAKE_LOCK_OPEN, true);
        if (aBoolean){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }


    private void initTabFragment() {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getFragment(),null);
        viewPager.setAdapter(viewPagerAdapter);

    }



    private void initNavigation() {
        List<NavigationItem> list = new ArrayList<>();
        list.add(new NavigationItem(R.mipmap.icon_script_selected,
                R.mipmap.icon_script_un_selected, "脚本"));
        list.add(new NavigationItem(R.mipmap.icon_setting_selected,
                R.mipmap.icon_setting_un_selected, "设置"));
        navigationView.bind(list,viewPager);
    }
    private void releaseWakeLock() {
        if (wakeLock == null) {
            return;
        }

        try {
            wakeLock.release();
        } catch (Exception e) {/* ignore */}
        wakeLock = null;
    }
    @Override
    protected int getLayoutID() {
        return R.layout.activity_main_script;
    }



}
